home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9944 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: A question on for loop
  5. Date: 14 Mar 1996 08:20:50 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4i9h12INN96u@keats.ugrad.cs.ubc.ca>
  8. References: <31471574.79D9@hdc-usa.com> <4i77ca$161@ccshst05.cs.uoguelph.ca>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4i77ca$161@ccshst05.cs.uoguelph.ca>,
  12. Toby K Hay <thay@uoguelph.ca> wrote:
  13.  >Phil Boyd (boydp@hdc-usa.com) wrote:
  14.  >: > |>
  15.  >: > |> for(i=0;i< 20; i++)
  16.  >: > |> {
  17.  >: > ...
  18.  >: > |>   if(condition==FALSE)
  19.  >: > |>   continue;
  20.  >: > ...
  21.  >: > |> }
  22.  >
  23.  >: OK, now for a newbie question just for my own edification:
  24.  >: Inside the for loop - does "i" start off with the value 0 so that
  25.  >: the loop has the potential to repeat up to 20 times (assuming that
  26.  >: "condition" remains FALSE)?
  27.  >
  28.  >My novice's answer is yes, i starts off at 0 because the for statement 
  29.  >(i=0;i<20;i++) has i++ meaning that i is incremented after the 
  30.  >operation.  If it were (i=0;i<20;++i) then i would start at 1 and run up 
  31.  >to 20.  Is this correct?
  32.  
  33. Totally false. The i++ is the ``operation'', not the entire for() statement.
  34. It makes no difference at all whether you write i++ or ++i, since the value
  35. of the ``i++'' expression is not used for anything; only the side effect of
  36. the ++ is relied upon.
  37.  
  38. Look at the grammar production of the for loop. It is something like this:
  39.  
  40.     for ( expression_opt ; expression_opt ; expression_opt ) statement
  41.  
  42. Each expression is evaluated individually, including all of its side effects.
  43. -- 
  44.  
  45.